package com.eshop.controller.good;

import com.eshop.pojo.Good;
import com.eshop.pojo.RespBean;
import com.eshop.pojo.RespPageBean;
import com.eshop.service.GoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "在售商品管理相关")
@RestController
@RequestMapping("/good/onsell")
public class GoodController {
    @Autowired
    GoodService goodService;


    //分页获取商品列表
    @ApiOperation("分页获取商品列表")
    @GetMapping("/")
    public RespPageBean getGoodListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                          @RequestParam(defaultValue = "8") Integer size,Good good) {
        return goodService.getGoodListByPage(currentPage, size,good);
    }

    //显示所有商品列表
//    @ApiOperation("获取所有商品")
//    @GetMapping("/all")
//    public List<Good> queryGoodList(){
//        return goodService.queryGoodList();
//    }

    @ApiOperation(value = "更新商品信息")
    @PutMapping("/")
    public RespBean updateGood(@RequestBody Good good){
        if (goodService.updateById(good) == 1){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败!");
    }

    //按照商品编号查询
    @ApiOperation("按照商品编号查询")
    @GetMapping("/{id}")
    public Good queryGoodById(@PathVariable int id){
        return goodService.queryGoodById(id);
    }

    //冻结商品
    @ApiOperation("冻结商品")
    @PutMapping("/freezeGood/{id}")
    public RespBean freezeGood(@PathVariable int id){
        if (goodService.freezeGood(id) == 1){
            return RespBean.success("冻结成功！");
        }
        return RespBean.error("冻结失败！");
    }

//    //恢复上架（解冻商品）
//    @ApiOperation("恢复上架（解冻商品）")
//    @PutMapping("/recoverGood/{id}")
//    public RespBean recoverGood(@PathVariable int id){
//        if (goodService.recoverGood(id) == 1){
//            return RespBean.success("解冻成功!");
//        }
//        return RespBean.error("解冻失败！");
//    }

    //已销售
    @ApiOperation("下架商品")
    @PutMapping("/removeGood/{id}")
    public RespBean removeGood(@PathVariable int id){
        if (goodService.removeGood(id) == 1){
            return RespBean.success("商品下架成功！");
        }
        return RespBean.error("商品下架失败！");
    }

    //查看历史销售商品
    @ApiOperation("查看历史销售商品")
    @GetMapping("/findSellGoodList")
    public List<Good> findSellGoodList(){
        return goodService.findSellGoodList();
    }

    //删除商品
    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public RespBean delGood(@PathVariable int id){
        if (goodService.delGood(id) == 1){
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    //发布商品
    @ApiOperation("发布商品")
    @PostMapping("/")
    public RespBean addGood(@RequestBody Good good){
        if (goodService.addGood(good) == 1){
            return RespBean.success("发布成功！");
        }
        return RespBean.error("发布失败！");
    }
}
